<?php

/**
 * 商户中心
 * @author wcl
 */

namespace Logistics\Controller;

use Think\Db;
use Think\Upload;

class BusinessCenterController extends BaseController {

    /**
     * 商家列表
     */
    public function index() {
        $info = M("logistics_supply")->find($this->_member_info['id']);
        $this->assign('user',$info);
        $this->display();
    }

    /**
     * 邮箱验证发送
     */
    public function send_mail(){
        $mail = $this->_post['mail'];
        $info = M("logistics_supply")->find($this->_member_info['id']);
        $res = think_send_mail($mail,'小波波','盛汇购','点击链接http://www.baidu.com');
        if($res == 1){
            $info['is_check_email'] = 2;
            M("logistics_supply")->save( $info );
            $data['status']  = 1;
            $data['content'] = '发送成功';
            $this->ajaxReturn($data);
        }else{
            $data['status']  = 1;
            $data['content'] = '发送失败';
            $this->ajaxReturn($data);
        }

    }

    public function mobileverify(){
        $mobile = $this->_get['mobile'];

        $this->assign('mobile',$mobile);
        $this->display();
    }

    public function obtain(){
        //手机号
        $mobile=I('post.mobile');
        $stat=$this->checkphone($mobile);
        if($stat==false){
            $this->error('手机号不规范');
            exit;
        }

        //判断发送次数
        $Maxcount = 3;
        $todaytime = strtotime(date("Y-m-d"));
        M()->startTrans();
        $Code = M("logistics_smscode");

        $where = array();
        $where['phone'] = $mobile;
        $where['sendtime'] = array('GT',$todaytime);
        $count = $Code->where($where)->count();
        if($count >= $Maxcount){
            $this->error('验证码发送频繁,请明天再试');
            exit;
        }else{
            //发送内容
            $y1=rand(0,9);
            $y2=rand(0,9);
            $y3=rand(0,9);
            $y4=rand(0,9);
            $y5=rand(0,9);
            $y6=rand(0,9);
            $num=$y1.$y2.$y3.$y4.$y5.$y6;
            $aaa= $Code->add(array(
                'phone'    => $mobile,
                'code'     => $num,
                'sendtime' => time()
            ));
            //对汉字进行转码
            $msg  =urlencode(mb_convert_encoding('您的验证码为【'.$num.'】',"gb2312","UTF-8"));
            //查询短信配置信息
            /*$sys=M('sysconfig')->where(array('config_name'=>'sms'))->field('config_text')->find();
            $sys=unserialize($sys['config_text']);
            //功能：发送短信
            //用户名
            $name = $sys['username'];
            //密码
            $pwd  = $sys['password'];
            //目标手机号
            $dst = $mobile;

            //地址
            $url=$sys['url']."?name=$name&pwd=$pwd&dst=$dst&msg=$msg";

            $status=$this->urlGet($url);
            //返回格式  num=1&success=电话&faile=&err=发送成功！&errid=0
            //parse_str 转换成数组
            parse_str($status, $arr);*/

            $arr['errid'] = 0;
            if($aaa && $arr['errid']==0){
                M()->commit();
                $this->success('发送成功');
            }else{
                M()->rollback();
                $this->error('发送失败');
            }
        }
    }



    //验证手机号
    public function checkphone($number){
        if(preg_match("/^1\d{10}$/",$number)){
            return true;
        }else{
            return false;
        }
    }



    //查询验证码
    public function checkver(){
        $mobile = I('post.mobile');
        $verify = I('post.code');
        $Code = M("logistics_smscode");
        $info = $Code->where(array('phone' => $mobile))->order("sendtime desc")->find();
        if( (time()-30*60) > $info['sendtime']){
            $this->error('验证码获取超时');
            exit;
        }

        if(!$info || $info['code'] != $verify){
            $this->error('验证码错误');
            exit;
        }
        $info = M("logistics_supply")->find($this->_member_info['id']);
        $info['is_check_mobile'] = 1;
        if(M("logistics_supply")->save( $info )){
            $this->success('认证成功');
        }else{
            $this->success('认证失败');
        }

    }

    public function companyauth(){
        $info = M("logistics_supply")->find($this->_member_info['id']);
        if ($_POST){
            if ($this->_post['license']){
                $imgs['license_url'] = $this->_post['license'];
            }
            if ($this->_post['certificate']){
                $imgs['certificate_url'] = $this->_post['certificate'];
            }
            if ($this->_post['permit']){
                $imgs['permit_url'] = $this->_post['permit'];
            }
            $imgs['id'] = $info['id'];
            $imgs['is_check_enterprise'] = 2;
            M("logistics_supply")->save( $imgs );
            $this->success('申请成功',U('Logistics/BusinessCenter/index'));
        }



        $this->assign('user',$info);
        $this->display();
    }

    /**
     * 文件上传
     */
    public function fileUpload(){
        $config = C('FILE_UPLOAD_CONFIG');
        //附带的信息
        $request_data = I('post.');

        // 上传文件

        //实例化上传类，传入上面的配置数组
        $uploader = new Upload($config, 'Local');
//            $uploader->saveName = $file_uuid;
        $info = $uploader->upload($_FILES);
//        show_bug_with_exit($info);
        //这里判断是否上传成功
        if ($info) {
            //// 上传成功 获取上传文件信息
            foreach ($info as &$file) {
                //拼接出上传目录
                $file['rootpath'] = __ROOT__ . ltrim($config['rootPath'], ".");
                //拼接出文件相对路径
                $file['filepath'] = $file['rootpath'] . $file['savepath'] . $file['savename'];
            }
            //这里可以输出一下结果,相对路径的键名是$info['upload']['filepath']
            $filepath = $file['filepath'];
//                show_bug_with_exit($filepath);
            $file_info['file_path'] = $filepath;

            $return_data['error'] = '文件上传成功';
            $return_data['img_path'] = $filepath;
            $this->ajaxReturn($return_data);
        } else {
            //输出错误信息
            $error_msg = $uploader->getError();
            $return_data['error'] = $error_msg;
            $this->ajaxReturn($return_data);
        }
    }

}
